knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE
)
# remotes::install_github("fishvice/tidyices", ref = "dev")
library(tidydatras) # remotes::install_github("fishvice/tidydatras")
library(tidyverse)
library(DescTools)  # e.g. winsorize function


spatialdir <- "C:/DATA/RDATA"
load(file.path(spatialdir, "rect.RData")) 
source("../../r/geo_inside.R")

# suppressMessages(tidydatras:::dr_download_all(save_dir="C:/TEMP", verbose=FALSE)) 

Get data and tidy

Note that the dr_getdata can work with multiple surveys (a loop is inside the function)

surveys <- c("NS-IBTS")
yrs <- 2020:2022 
qs  <- c(3)

hh <- dr_getdata("HH", surveys, yrs, qs, quiet = FALSE) %>% 
  dr_tidy() %>%                             # Make tidy with column specifications
  dr_idunite(., remove = FALSE) %>%         # Add identifier
  left_join(reco, by=c("ship"="code"))      # Add vesselname

hl <- dr_getdata("HL", surveys, yrs, qs) %>% 
  dr_tidy() %>%                             # Make tidy with column specifications
  dr_idunite(., remove = FALSE) %>%         # Add identifier
  dr_calccpue(hh) %>%                       # Calculated CPUE per hour
  left_join(aphia_latin) %>%                # Add scientific name
  left_join(asfis) %>%                      # Add FAO species names and codes
  left_join(reco, by=c("ship"="code"))      # Add vesselname


# ca <- dr_getdata("CA", surveys, yrs, qs, quiet=FALSE) %>%  
#   dr_tidy() %>%                             # Make tidy with column specifications
#   dr_idunite(., remove = FALSE) %>%         # Add identifier
#   dr_calccpue(hh) %>%                       # Calculated CPUE per hour
#   left_join(aphia_latin) %>%                # Add scientific name
#   left_join(asfis) %>%                      # Add FAO species names and codes
#   left_join(reco, by=c("ship"="code"))      # Add vesselname
hh |> count(survey)
hl |> count(survey)
ca |> count(survey)

Species - workflow experiments

hl |> 
  left_join(aphia_latin) |> 
  count(latin)
ca |> 
  left_join(aphia_latin) |> 
  count(latin)

Length frequencies plots

select_species <- "HER"
select_survey  <- "NS-IBTS"
select_quarter <- 1
winsorize      <- c(0.05, 0.95)
length_step    <- 0.5

df <-
  hl %>% 
  filter(species == select_species) %>% 
  filter(survey == select_survey) %>% 
  filter(quarter == select_quarter) %>%
  drop_na(cpue_number_per_hour, length) %>% 
  mutate(length=floor(1/length_step * length)/(1/length_step)) %>% 
  {if(!is.null(winsorize)) {mutate(., cpue_number_per_hour = DescTools::Winsorize(cpue_number_per_hour, probs=winsorize))}} %>% 
  group_by(species, latin, year, quarter, statrec, length) %>% 
  summarise(cpue_number_per_hour = mean(cpue_number_per_hour)) %>% 
  group_by(species, latin, year, quarter, length) %>% 
  summarise(cpue_number_per_hour = mean(cpue_number_per_hour)) 

yearmean <-
  df %>% 
  group_by(species, latin, quarter, length) %>% 
  summarise(cpue_number_per_hour = mean(cpue_number_per_hour)) 

df %>% 
  ggplot(aes(x=length, y=cpue_number_per_hour)) +
  theme_minimal() +
  geom_bar(data=yearmean, stat="identity", fill="gray", alpha=0.5) +
  geom_line() +
  facet_wrap(~year, strip.position = "right", dir="v", ncol=3)


fishvice/tidyices documentation built on Sept. 12, 2023, 4:22 p.m.